Unicef Data Viz

Running Code

When you click the Render button a document will be generated that includes both content and the output of embedded code. You can embed code like this:

install.packages("rnaturalearth")
Installing package into '/cloud/lib/x86_64-pc-linux-gnu-library/4.3'
(as 'lib' is unspecified)
library(rnaturalearth)
library("ggplot2")
unicef_metadata <- read.csv("./unicef_metadata.csv")

ggplot(unicef_metadata, aes(y=life_expectancy, x=year)) +
  geom_point(alpha=0.5) +
  geom_smooth(method = "lm", se = FALSE)
`geom_smooth()` using formula = 'y ~ x'
Warning: Removed 1079 rows containing non-finite values (`stat_smooth()`).
Warning: Removed 1079 rows containing missing values (`geom_point()`).

Bar Chart

unicef_indicator <- read.csv("./unicef_indicator.csv")

data = unicef_indicator[unicef_indicator$time_period > 2003 & unicef_indicator$time_period < 2012, ]

ggplot(data, aes(x=time_period, y=obs_value, fill = sex)) +
  geom_bar(position = "stack",
           stat = "summary",
           fun = "mean")

Scatter Plot

library("dplyr")

Attaching package: 'dplyr'
The following objects are masked from 'package:stats':

    filter, lag
The following objects are masked from 'package:base':

    intersect, setdiff, setequal, union
data2 <- unicef_indicator %>%
  group_by(time_period)%>%
  summarize(mean=mean(obs_value))

ggplot(data2,aes(x=time_period,y=mean)) +
  geom_point(alpha=0.5) +
  geom_smooth(method = "lm", se = FALSE)
`geom_smooth()` using formula = 'y ~ x'

# Load required libraries

library(rnaturalearth)

install.packages("rnaturalearthdata")
Installing package into '/cloud/lib/x86_64-pc-linux-gnu-library/4.3'
(as 'lib' is unspecified)
library(plotly)

Attaching package: 'plotly'
The following object is masked from 'package:ggplot2':

    last_plot
The following object is masked from 'package:stats':

    filter
The following object is masked from 'package:graphics':

    layout
# Load viridis package for additional color palettes
install.packages("viridis")
Installing package into '/cloud/lib/x86_64-pc-linux-gnu-library/4.3'
(as 'lib' is unspecified)
library(viridis)
Loading required package: viridisLite
# Get a spatial dataset of country boundaries
world_map <- ne_countries(scale = "medium", returnclass = "sf")

# Assign the main dataset to a new variable with a simpler name
main_data <- `unicef_indicator`

# Merge the main dataset (main_data) with the world_map dataset
merged_data <- world_map %>%
  left_join(main_data, by = c("name" = "country"))

# Create a ggplot object with merged_data
world_map_plot <- ggplot(data = merged_data) +
  geom_sf(aes(fill = obs_value, text = paste("Obs_value:", obs_value, "<br>Country:", name)), size = 0.1) +
  theme_minimal() +
  scale_fill_viridis_c(option = "A", direction = -1, trans = "log", 
                       name = "Observation\nValue", labels = scales::comma, end = 0.9) +
  labs()
Warning in layer_sf(geom = GeomSf, data = data, mapping = mapping, stat = stat,
: Ignoring unknown aesthetics: text
# Convert to interactive plotly object
interactive_world_map <- ggplotly(world_map_plot)

# Include the interactive plot in R Markdown document
interactive_world_map